/**
 * 
 */
package fr.univnantes.lina.ftbcleaner;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * Handling the interaction with the FTB as DOM structure 
 * 
 * @author hernandez
 * 
 * Java XPath (Apache JAXP implementation) performance
 * http://stackoverflow.com/questions/8285003/optimizing-dom-and-xpath-java-code
 * http://stackoverflow.com/questions/6340802/java-xpath-apache-jaxp-implementation-performance
 * -Dorg.apache.xml.dtm.DTMManager=
  org.apache.xml.dtm.ref.DTMManagerDefault
 *
 */
public final class FTBXMLVariousDOMHandler {

	//String expression = "//w[descendant::w[@catint or @cat] and contains(@lemma,' ')]"; // 26714 # occ from original corpus
	//String expression = "//w[descendant::w[@catint] and contains(@lemma,' ')]"; // 25867 # occ from original corpus
	//String expression = "//w[descendant::w[@cat] and contains(@lemma,' ')]"; // 861 # occ from original corpus
	//String expression = "//w[descendant::w[@cat]]"; // 1522 # occ from original corpus
	/**
	 *  part of multi words have a cat attribute instead of catint
	 */
	static final String partOfMWWiCatAttributeXPathExpression = "//w[ancestor::w and @cat]"; // 3830 # occ from original corpus

	 /**
	  * multiwords where coumpound attribute is missing 
	 */
	static final	String mWWiCompoundAttributeMissionXPathexpression = "//w[descendant::w]"; 
	
	/**
	 *  mw not a graphical unit
	 *  	// we do not need to work with mw which are not graphical units, since we also consider them as token... 
	 */
 	static final String mWNotAGraphicUnitXPathExpression = "//w[descendant::w[@catint] and contains(@lemma,' ')]"; // 26714 # occ from original corpus

	/**
	 *  simple words with content (no amalgame)  
	 */
	static final String swNotAmalgameXPathExpression = "//w[not(@compound) and @cat and not(normalize-space(.)='')]";
	//String swNotAmalgameXPathExpression = "//w[not(parent::w) and not(descendant::w) and not(normalize-space(.)='')]";
	
	/**
	 *  single words or part of words (potentially amalgame) but not multi words  
	 */
	static final String notAMWXPathExpression = "//w[not(@compound)]";
	//String swNotAmalgameXPathExpression = "//w[not(parent::w) and not(descendant::w) and not(normalize-space(.)='')]";
	


	/**
	 *  part of multi words have a cat attribute instead of catint
	 */
	public static NodeList getNodeListFromXPathExpression(Node domDoc, String expression) { 
		XPath xpath = XPathFactory.newInstance().newXPath();


		NodeList nodeList  = null;
		try {
			nodeList = (NodeList) xpath.evaluate(expression, domDoc, XPathConstants.NODESET);

		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nodeList;
	}


}
